/**
 * @file app_main.c
 * @brief 
 * @author yejing (yejh@gongniu.cn)
 * @version 1.0
 * @date 2024-05-15
 * 
 * Copyright 2024-2025 GongNiu Inc. All Rights Reserved.
 * 
 */

#include <stdio.h>

#include "bull_hal_sys_thread.h"
#include "bull_hal_sys_timer.h"
#include "bull_hal_sys_system.h"
#include "bull_hal_drv_wifi.h"
#include "bull_hal_net_https.h"
#include "bull_hal_sys_memory.h"
#include "esp_log.h"

static const char* TAG = "https_test";
static HTTPS_CLIENT_HANDLE https_hadnle = NULL;


const char *productKey        = "g8ujBWh59Oc";
const char *device_name       = "C411E101526E";
const char *Device_secret     = "dcb7c39d07fa9f3be39c98b06b5b7896";

typedef struct 
{
    char        *productKey;
    char        *deviceName;
    TIME_T       timestamp;
    char*        clientId;
    char*        sign;
    char*        signmethod;
    char*        iotToken;
}bull_https_post_data_t;

static char clientId[ 42 ] = {0};
static char hmacsha1_sign[40+1] = {0};
static char  device_secret[32+1] = {0};

/**
 * @brief 
 * 
 * @param param 
 */
static void app_init_thread(void* param)
{

    bool first_init = true;

    BULL_HTTPS_REGISTER_T reg = {
        .url               = "www.baidu.com",
        .method            = BULL_HTTP_METHOD_POST,
        .data_recv_cb      = NULL,
        .userata           = "hello",
    };

    while ( 1 ){
        
        bull_hal_sys_sleep( 1000 );

        NW_IP_S ip = { 0 };
        bull_hal_drv_wifi_get_ip( WF_STATION, &ip );
        ESP_LOGI( TAG,"ip:%s gw:%s mask:%s", ip.ip, ip.gw, ip.mask );

        if ( strlen( ip.mask) != 0 && true == first_init )
        {
           first_init = false;
           bull_hal_net_https_register( &reg, https_hadnle );

           bull_hal_net_https_client_perform( https_hadnle );
        }
        
    }
}



/**
 * @brief 
 * 
 */
static void app_init_entry(void)
{

    bull_hal_drv_wifi_init( NULL );

    // wifi作为sta模式连接路由器
    bull_hal_drv_wifi_station_connect((const char *)"GN-XIU",(const char *)"88888888");

    bull_hal_sys_thread_create( app_init_thread,
    "app_init_thread",
    4*4096 ,
    NULL,
    10,
    NULL);

}

/**
 * @brief 
 * 
 */
void app_main(void)
{

    app_init_entry();

}
